﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;

namespace AnyOffice.general.meeting.query
{
    public partial class index : ModulePage
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.txtStartTimeBegin.Value = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-1";
                this.txtStartTimeEnd.Value = DateTime.Now.ToString("yyyy-MM-dd");
                DataTable table = new MeetingRoomDao().SelectAll(base.Conn);
                ListItem item = new ListItem("全部", "");
                this.ddlRoomId.Items.Add(item);
                if (table.Rows.Count > 0)
                {
                    item = new ListItem(table.Rows[0]["Name"].ToString(), table.Rows[0]["RoomId"].ToString());
                    this.ddlRoomId.Items.Add(item);
                }
            }
            if (this.Page.IsPostBack)
            {
                this.litNavTitle.Text = "会议查询结果";
                this.pnlMain.Visible = false;
                this.pnlQuery.Visible = true;
                SqlCommand command = new SqlCommand();
                base.Conn.Open();
                command.Connection = base.Conn;
                string str = "SELECT [Meeting].[MeetingId],[Meeting_Room].[Name] AS RoomName,[Meeting].[Name],[Meeting].[Topic],[Users].[UserName] AS ProposerName,[Department].[DeptName],[Meeting].[StartTime],[Meeting].[EndTime],[Meeting].[Status] FROM [dbo].[Meeting],[dbo].[Meeting_Room],[dbo].[Users],[dbo].[Department] ";
                str = str + " WHERE [Meeting].[RoomId] = [Meeting_Room].[RoomId] AND [Meeting].[Proposer] = [Users].[UserId] AND [Users].[DeptId] = [Department].[DeptId] ";
                if (this.txtName.Value != string.Empty)
                {
                    str = str + " AND [Meeting].[Name] LIKE '%' + @Name + '%' ";
                    command.Parameters.Add("@Name", this.txtName.Value);
                }
                if (this.txtTopic.Value != string.Empty)
                {
                    str = str + " AND [Meeting].[Topic] LIKE '%' + @Topic + '%' ";
                    command.Parameters.Add("@Topic", this.txtTopic.Value);
                }
                if (this.ToId.Value != string.Empty)
                {
                    str = str + " AND [Meeting].[Proposer] = @UserId ";
                    command.Parameters.Add("@UserId", this.ToId.Value);
                }
                if (this.txtStartTimeBegin.Value != string.Empty)
                {
                    str = str + " AND [Meeting].[StartTime] >= @StartTimeBegin ";
                    command.Parameters.Add("@StartTimeBegin", Convert.ToDateTime(this.txtStartTimeBegin.Value + " 00:00:00"));
                }
                if (this.txtStartTimeEnd.Value != string.Empty)
                {
                    str = str + " AND [Meeting].[EndTime] <= @StartTimeEnd ";
                    command.Parameters.Add("@StartTimeEnd", Convert.ToDateTime(this.txtStartTimeEnd.Value + " 23:59:59"));
                }
                if (this.ddlRoomId.SelectedValue != string.Empty)
                {
                    str = str + " AND [Meeting].[RoomId] = @RoomId ";
                    command.Parameters.Add("@RoomId", Convert.ToInt32(this.ddlRoomId.SelectedValue));
                }
                str = str + " ORDER BY [Meeting].[MeetingId] DESC";
                command.CommandText = str;
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    this.rptMeeting.DataSource = reader;
                    this.rptMeeting.DataBind();
                    reader.Close();
                }
                else
                {
                    this.litMessage.Text = base.ShowMessage("无符合条件的会议");
                }
            }
        }
 

    }
}
